import {createRouter, createWebHistory} from "vue-router";
import Index from "../view/Index.vue";
import Home from "../view/user/home/Home.vue"
import Login from "../view/login/Login.vue";
import Registry from "../view/login/Registry.vue";
import BackPassword from "../view/login/BackPassword.vue";
import AdminGradeInfo from "../view/admin/grade/Info.vue";
import AdminGradeView from "../view/admin/grade/View.vue";
import AdminLayout from "../view/admin/Index.vue";
import {ElMessage} from "element-plus";
import NoticePublishIndex from "../view/admin/notice/NoticePublishIndex.vue";
import NoticeIndex from "../view/admin/notice/Index.vue";
import ExemptionIndex from "../view/admin/exemption/Index.vue";
import ExemptionAuditIndex from "../view/admin/exemption/ExemptionAuditIndex.vue";
import MakeUpIndex from "../view/admin/makeup/Index.vue";
import MakeUpAuditIndex from "../view/admin/makeup/MakeUpAuditIndex.vue";
import TrainingContentIndex from "../view/admin/training/TrainingContentIndex.vue";
import TrainingIndex from "../view/admin/training/Index.vue";
import TestGuideIndex from "../view/admin/testGuide/TestGuideIndex.vue"
import TestIndex from "../view/admin/testGuide/Index.vue"
import Notice from "../view/user/student/Notice.vue";
import Grades from "../view/user/student/Grades.vue";
import Exemption from "../view/user/student/Exemption.vue";
import Makeup from "../view/user/student/Makeup.vue";
import Training from "../view/user/student/Training.vue";
import Testguide from "../view/user/student/Testguide.vue";
import Feedback from "../view/user/student/Feedback.vue";
import FeedbackIndex from "../view/admin/feedback/Index.vue";
import FeedbackAdminIndex from "../view/admin/feedback/FeedbackAdminIndex.vue";
import BackPasswordNew from "../view/login/BackPasswordNew.vue";

// 修改管理员模块的组件导入路径

// 子页面组件

//meta元数据 title用于面包屑展示。
const routes = [
    {//一进来登录
        path: '/',
        redirect: '/login'
    },
    {//登录页
        path: '/login',
        name: 'login',
        component: Login,
    },
    {//注册页
        path: '/registry',
        name: 'registry',
        component: Registry,
    },
    {//找回密码
        path: '/backPassword',
        name: 'backPassword',
        component: BackPassword,
    },
    {//找回密码[账号密码找回]
        path: '/backPasswordNew',
        name: 'backPasswordNew',
        component: BackPasswordNew,
    },
    {//主体框架
        path: '/index',
        name: 'index',
        component: Index,
        meta: {title: '首页'},
        redirect: '/home', //默认组件展示，切到index组件的时候，就去显示地址为/home的组件
        children: [ //子路由 点击左侧导航栏跳转到指定子路由
            {//首页
                path: '/home',
                component: Home,
                name: 'home',
                meta: {title: '首页'},
            },
            {
                path: '/notice',
                component: Notice,
                name: 'notice',
                meta: {title: '发布通知'},
            },
            {
                path: '/grades',
                component: Grades,
                name: 'grades',
                meta: {title: '查看成绩'},
            },
            {
                path: '/exemption',
                component: Exemption,
                name: 'exemption',
                meta: {title: '免测申请'},
            },
            {
                path: '/makeup',
                component: Makeup,
                name: 'makeup',
                meta: {title: '补考报名'},
            },
            {
                path: '/training',
                component: Training,
                name: 'training',
                meta: {title: '训练攻略'},
            },
            {
                path: '/testguide',
                component: Testguide,
                name: 'testguide',
                meta: {title: '测试指南'},
            },
            {
                path: '/feedback',
                component: Feedback,
                name: 'feedback',
                meta: {title: '意见反馈'},
            }
        ]
    },
    // 添加管理员模块路由
    {
        path: '/admin',
        component: AdminLayout,
        meta: { requiresAdmin: true },
        redirect: '/admin/notice',
        children: [
            {
                path: '/admin/notice',
                name: 'NoticeIndex',
                component: NoticeIndex,
                meta: {title: '发布通知'},
                children: [
                    {
                        path: '/admin/notice',
                        component: NoticePublishIndex,
                        name: 'noticePublish',
                        meta: { title: '发布' }
                    }
                ]
            },
            {
                path: '/admin/grade',
                component: AdminGradeInfo,
                meta: { title: '成绩录入管理' },
                children: [
                    {
                        path: '/admin/grade/list',
                        component: AdminGradeView,
                        name: 'gradeList',
                        meta: { title: '成绩列表' }
                    }
                ]
            },
            {
                path: '/admin/exemption',
                name: 'ExemptionIndex',
                component: ExemptionIndex,
                meta: {title: '免测申请列表'},
                children: [
                    {
                        path: '/admin/exemption',
                        component: ExemptionAuditIndex,
                        name: 'ExemptionAuditIndex',
                        meta: { title: '审核' }
                    }
                ]
            },
            {
                path: '/admin/makeup',
                name: 'MakeUpIndex',
                component: MakeUpIndex,
                meta: {title: '补考申请列表'},
                children: [
                    {
                        path: '/admin/makeup',
                        component: MakeUpAuditIndex,
                        name: 'MakeUpAuditIndex',
                        meta: { title: '审核' }
                    }
                ]
            },
            {

                path: '/admin/training',
                name: 'TrainingIndex',
                component: TrainingIndex,
                meta: {title: '训练指导列表'},
                children: [
                    {
                        path: '/admin/training',
                        component: TrainingContentIndex,
                        name: 'TrainingContentIndex',
                        meta: { title: '指导列表' }
                    }
                ]
            },
            {

                path: '/admin/testguide',
                name: 'TestIndex',
                component: TestIndex,
                meta: {title: '测试指南列表'},
                children: [
                    {
                        path: '/admin/testguide',
                        component: TestGuideIndex,
                        name: 'TestGuideIndex',
                        meta: { title: '测试指南' }
                    }
                ]
            },
            {

                path: '/admin/feedback',
                name: 'FeedbackIndex',
                component: FeedbackIndex,
                meta: {title: '反馈列表'},
                children: [
                    {
                        path: '/admin/feedback',
                        component: FeedbackAdminIndex,
                        name: 'FeedbackAdminIndex',
                        meta: { title: '查看反馈' }
                    }
                ]
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 添加全局路由守卫进行权限验证
router.beforeEach((to, from, next) => {
    const userRole = localStorage.getItem('userRole')

    // 检查是否需要管理员权限
    if (to.matched.some(record => record.meta.requiresAdmin)) {
        if (userRole === '1') {
            next()
        } else {
            ElMessage.error('无权限访问该页面')
            next('/home')
        }
    } else {
        next()
    }
})

export default router;
